from lxml.html import etree
import re

class HtmlParser:
    def parser(self, page_url, html_text):
        """
        解析页面新的 url 链接和数据
        :param page_url: url
        :param html_text: 页面内容
        :return: tuple / None
        """
        if not page_url and not html_text:
            return None
        new_urls = self._get_new_urls(page_url, html_text)
        new_data = self._get_new_data(html_text)

        return new_urls, new_data

    def _get_new_urls(self, page_url, html_text):
        """
        返回解析后的 url 集合
        :param page_url: url
        :param html_text: 页面内容
        :return: set
        """
        new_urls = set()
        links = re.compile(r'\?start=\d+').findall(html_text)
        for link in links:
            new_urls.add(page_url.split('?')[0] + link)
        return new_urls

    def _get_new_data(self, html_text):
        """
        返回解析后的数据列表
        :param html_text: 页面内容
        :return: list
        """
        datas = []
        for html in etree.HTML(html_text).xpath('//ol[@class="grid_view"]/li'):
            name = html.xpath('./div/div[@class="info"]/div[@class="hd"]/a/span[1]/text()')[0]
            score = html.xpath('./div/div[@class="info"]/div[@class="bd"]/div[@class="star"]/span[2]/text()')[0]
            person_num = html.xpath('./div/div[@class="info"]/div[@class="bd"]/div[@class="star"]/span[4]/text()')[0].strip('人评价')
            datas.append([name, score, person_num])
        return datas